package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.settlement.system.common.util.NumberUtils;
import com.settlement.system.model.entity.SysBalance;
import com.settlement.system.mapper.SysBalanceMapper;
import com.settlement.system.service.SysBalanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.SysBalanceQuery;
import com.settlement.system.model.form.SysBalanceForm;
import org.springframework.beans.BeanUtils;
import com.settlement.system.model.vo.SysBalanceImportVo;

/**
 * <p>
 * 余额表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-07-29
 */
@Service
public class SysBalanceServiceImpl extends ServiceImpl<SysBalanceMapper, SysBalance> implements SysBalanceService {

    @Override
    public Page<SysBalance> getPage(SysBalanceQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysBalance> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveSysBalance(SysBalanceForm sysBalanceForm) {
         SysBalance sysBalance = new SysBalance();
         BeanUtils.copyProperties(sysBalanceForm, sysBalance);
         sysBalance.setCreateTime(null);
         return this.saveOrUpdate(sysBalance);
     }

     @Override
     public SysBalanceForm getSysBalanceFormData(Long id) {
         SysBalance sysBalance = this.getById(id);
         SysBalanceForm sysBalanceForm = new SysBalanceForm();
         BeanUtils.copyProperties(sysBalance, sysBalanceForm);
         return sysBalanceForm;
     }

     @Override
     public boolean deleteSysBalance(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<SysBalanceImportVo> voList)  {
        List<SysBalance> sysBalanceList = new ArrayList<>();
        for (SysBalanceImportVo importVo : voList) {
            SysBalance sysBalance = new SysBalance();
            BeanUtils.copyProperties(importVo, sysBalance);
            sysBalanceList.add(sysBalance);
        }
        this.saveOrUpdateBatch(sysBalanceList);
    }

    @Override
    public List<SysBalanceImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysBalance> sysBalanceList = this.getBaseMapper().selectBatchIds(ids);
        List<SysBalanceImportVo> voList = new ArrayList<>();
        for (SysBalance sysBalance : sysBalanceList) {
            SysBalanceImportVo vo = new SysBalanceImportVo();
            BeanUtils.copyProperties(sysBalance, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public boolean updateBalance(SysBalanceForm sysBalanceForm) {
        SysBalance sysBalance = this.getOne(new LambdaQueryWrapper<SysBalance>().eq(SysBalance::getStoreId, sysBalanceForm.getStoreId()).eq(SysBalance::getBillDate, sysBalanceForm.getBillDate()));
        if(Objects.isNull(sysBalance)){
            sysBalance = new SysBalance();
            sysBalance.setStoreId(sysBalanceForm.getStoreId());
            sysBalance.setBillDate(sysBalanceForm.getBillDate());
        }
        // 更新余额
        sysBalance.setBalanceAmount(NumberUtils.convertBigDecimal(sysBalanceForm.getBalanceAmount()));
        return this.saveOrUpdate(sysBalance);
    }
}
